package com.bom_samaritano.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.bom_samaritano.Exception.BusinessException;
import com.bom_samaritano.model.Route;
import com.bom_samaritano.model.VOGeneric;

public class DataAdapter implements DAO<VOGeneric> {

	private static DataAdapter dataAdapter;
	private SQLiteDatabase db;
	private DBHelper dbHelper = null;

	private DataAdapter(Context context) {
		dbHelper = new DBHelper(context);
		// Person p = new Person();
		// p.setNome("Thiago Carvalho");
		// p.setEmail("thiago.cmv@gmail.com");
		// this.insert(p);
	}

	public static DataAdapter getInstance(Context context) {
		if (dataAdapter == null) {
			dataAdapter = new DataAdapter(context);
		}
		return dataAdapter;
	}

	@Override
	public void insert(VOGeneric vo) {

		ContentValues cv = new ContentValues();

		for (Map.Entry<String, Object> entry : vo.getMap().entrySet()) {

			cv.put(entry.getKey(), String.valueOf(entry.getValue()));
		}
		this.db = dbHelper.getWritableDatabase();
		this.db.insert(vo.TABLE, null, cv);
		this.db.close();

	}

	@Override
	public void update(VOGeneric vo, long id) {
		ContentValues cv = new ContentValues();

		for (Map.Entry<String, Object> entry : vo.getMap().entrySet()) {

			cv.put(entry.getKey(), String.valueOf(entry.getValue()));
		}
		this.db = dbHelper.getWritableDatabase();
		this.db.update(vo.TABLE, cv, vo.COL_ID + " = ",
				new String[] { String.valueOf(id) });
		this.db.close();

	}

	@Override
	public Collection<VOGeneric> select(String table, String[] cols) {
		Collection<VOGeneric> collection = new ArrayList<VOGeneric>();
		this.db = dbHelper.getReadableDatabase();
		Cursor cursor = this.db
				.query(table, cols, null, null, null, null, null);

		cursor.moveToFirst();

		while (!cursor.isAfterLast()) {
			VOGeneric vo = new VOGeneric(table, "id");
			for (int i = 0; i < cols.length; i++) {
				vo.putParam(cols[i], cursor.getString(i));
			}

			collection.add(vo);

			cursor.moveToNext();
		}
		this.db.close();
		return collection;

	}

	@Override
	public void delete(VOGeneric object) {
		// TODO Auto-generated method stub
	}

	@Override
	public Collection<Route> dataRoute(Route route) {
		this.db = dbHelper.getReadableDatabase();
		Collection<Route> collection = new ArrayList<Route>();
		// getAll
		if (route == null) {

			Cursor cursor = this.db.query(Route.TABLE, new String[] {
					Route.COL_ID, Route.COL_LATITUDES, Route.COL_LONGITUDES,
					Route.COL_VACANCY, Route.COL_OWNER, Route.COL_NAME }, null,
					null, null, null, null);

			cursor.moveToFirst();

			while (!cursor.isAfterLast()) {
				Route r = new Route(
						cursor.getInt(cursor.getColumnIndex(Route.COL_ID)),
						cursor.getString(cursor.getColumnIndex(Route.COL_NAME)),
						cursor.getString(cursor.getColumnIndex(Route.COL_OWNER)),
						cursor.getInt(cursor.getColumnIndex(Route.COL_VACANCY)));
				try {
					r.addPoints(r.stringToPoints(cursor.getString(cursor
							.getColumnIndex(Route.COL_LATITUDES)), cursor
							.getString(cursor
									.getColumnIndex(Route.COL_LONGITUDES))));
				} catch (BusinessException e) {
					// TODO Auto-generated catch block
				}

				collection.add(r);

				cursor.moveToNext();
			}

		}
		// String query;
		// Collection<Route> routes = db.;
		// Collection<VOGeneric> retorno = new ArrayList<VOGeneric>();
		// // boolean isRotaCompativel = false;
		// for (Route iRoute : routes) {
		// // Adiciona ao retorno a rota seguido do dono
		// retorno.add(iRoute);
		// retorno.add(db.getPerson(iRoute.getOwnerEmail()));
		// }
		// return retorno;
		this.db.close();
		return collection;
	}
}